namespace RAG.Application.Dtos.Rag;

/// <summary>
/// RAG问答请求DTO
/// </summary>
public class RagQuestionAnswerRequestDto
{
    /// <summary>
    /// 用户问题
    /// </summary>
    public string Question { get; set; } = default!;

    /// <summary>
    /// 用户ID（可选）
    /// </summary>
    public Guid? UserId { get; set; }

    /// <summary>
    /// 会话ID（可选）
    /// </summary>
    public Guid? SessionId { get; set; }

    /// <summary>
    /// 返回的相似知识数量限制
    /// </summary>
    public int KnowledgeLimit { get; set; } = 5;

    /// <summary>
    /// 相似度阈值（0-1之间）
    /// </summary>
    public double SimilarityThreshold { get; set; } = 0.7;

    /// <summary>
    /// 聊天历史（可选）
    /// </summary>
    public List<string>? ChatHistory { get; set; }

    /// <summary>
    /// 当知识库中没有相关信息时，是否允许Chat模型直接回答问题
    /// </summary>
    public bool AllowDirectAnswer { get; set; } = false;
}

/// <summary>
/// RAG问答响应DTO
/// </summary>
public class RagQuestionAnswerResponseDto
{
    /// <summary>
    /// AI生成的回答
    /// </summary>
    public string Answer { get; set; } = default!;

    /// <summary>
    /// 用于生成回答的知识来源
    /// </summary>
    public List<KnowledgeSourceDto> KnowledgeSources { get; set; } = new();

    /// <summary>
    /// 处理时间（毫秒）
    /// </summary>
    public long ProcessingTimeMs { get; set; }

    /// <summary>
    /// 是否找到相关知识
    /// </summary>
    public bool HasRelevantKnowledge { get; set; }

    /// <summary>
    /// 回答来源类型
    /// </summary>
    public string AnswerSource { get; set; } = "knowledge_base"; // "knowledge_base" | "direct_answer" | "no_answer"

    /// <summary>
    /// 回答来源说明
    /// </summary>
    public string? SourceDescription { get; set; }
}

/// <summary>
/// 流式回答前的准备结果（提前拿到检索与上下文，加快首包速度）
/// </summary>
public class RagPreparationResult
{
    public string Question { get; set; } = default!;
    public List<KnowledgeSourceDto> KnowledgeSources { get; set; } = new();
    public string Context { get; set; } = string.Empty; // 知识拼接后的上下文
    public bool HasRelevantKnowledge { get; set; }
    public string AnswerSource { get; set; } = "knowledge_base"; // knowledge_base | direct_answer | no_answer
    public string? SourceDescription { get; set; }
    public bool StreamAnswer { get; set; } = true; // 是否进入流式生成
    public string? ImmediateAnswer { get; set; } // 若无需流式，直接返回的答案（no_answer 等）
    public string? DirectAnswerContext { get; set; } // direct_answer 场景用的提示上下文
}

/// <summary>
/// 知识来源DTO
/// </summary>
public class KnowledgeSourceDto
{
    /// <summary>
    /// 知识ID
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// 知识标题
    /// </summary>
    public string Title { get; set; } = default!;

    /// <summary>
    /// 知识内容
    /// </summary>
    public string Content { get; set; } = default!;

    /// <summary>
    /// 相似度得分
    /// </summary>
    public double SimilarityScore { get; set; }

    /// <summary>
    /// 来源文档
    /// </summary>
    public string? SourceDocument { get; set; }

    /// <summary>
    /// 文档类型
    /// </summary>
    public string? DocumentType { get; set; }
}

/// <summary>
/// 相似知识DTO
/// </summary>
public class SimilarKnowledgeDto
{
    /// <summary>
    /// 知识ID
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// 知识标题
    /// </summary>
    public string Title { get; set; } = default!;

    /// <summary>
    /// 知识内容
    /// </summary>
    public string Content { get; set; } = default!;

    /// <summary>
    /// 相似度得分
    /// </summary>
    public double SimilarityScore { get; set; }

    /// <summary>
    /// 来源文档信息
    /// </summary>
    public string? SourceInfo { get; set; }
}

/// <summary>
/// 知识上传请求DTO
/// </summary>
public class KnowledgeUploadRequestDto
{
    /// <summary>
    /// 知识标题
    /// </summary>
    public string Title { get; set; } = default!;

    /// <summary>
    /// 知识内容
    /// </summary>
    public string Content { get; set; } = default!;

    /// <summary>
    /// 文档类型
    /// </summary>
    public string DocumentType { get; set; } = ".txt";

    /// <summary>
    /// 嵌入类型（统一使用多模态模型）
    /// </summary>
    public string EmbeddingType { get; set; } = "multimodal-embedding";

    /// <summary>
    /// 图片URL（可选）
    /// </summary>
    public string? ImageUrl { get; set; }

    /// <summary>
    /// 是否分块
    /// </summary>
    public bool IsChunked { get; set; } = false;

    /// <summary>
    /// 元数据（JSON格式）
    /// </summary>
    public string? Metadata { get; set; }

    /// <summary>
    /// 来源信息
    /// </summary>
    public string? Source { get; set; }

    /// <summary>
    /// 用户ID（可选）
    /// </summary>
    public Guid? UserId { get; set; }
}

/// <summary>
/// 多模态知识上传请求DTO
/// </summary>
public class MultimodalKnowledgeUploadRequestDto
{
    /// <summary>
    /// 标题
    /// </summary>
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 文本内容（可选，与图片一起作为多模态内容）
    /// </summary>
    public string? Content { get; set; }

    /// <summary>
    /// 图片Base64数据
    /// </summary>
    public string? ImageBase64 { get; set; }

    /// <summary>
    /// 图片URL（与ImageBase64二选一）
    /// </summary>
    public string? ImageUrl { get; set; }

    /// <summary>
    /// 图片文件名（可选）
    /// </summary>
    public string? ImageFileName { get; set; }

    /// <summary>
    /// 文档类型
    /// </summary>
    public string DocumentType { get; set; } = ".jpg";

    /// <summary>
    /// 嵌入类型（固定为multimodal-embedding）
    /// </summary>
    public string EmbeddingType { get; set; } = "multimodal-embedding";

    /// <summary>
    /// 元数据（JSON格式）
    /// </summary>
    public string? Metadata { get; set; }

    /// <summary>
    /// 来源信息
    /// </summary>
    public string? Source { get; set; } = "API多模态上传";

    /// <summary>
    /// 用户ID（可选）
    /// </summary>
    public Guid? UserId { get; set; }
}

/// <summary>
/// 知识上传响应DTO
/// </summary>
public class KnowledgeUploadResponseDto
{
    /// <summary>
    /// 上传的知识ID
    /// </summary>
    public Guid KnowledgeId { get; set; }

    /// <summary>
    /// 文档ID
    /// </summary>
    public Guid DocumentId { get; set; }

    /// <summary>
    /// 文档块ID
    /// </summary>
    public Guid ChunkId { get; set; }

    /// <summary>
    /// 是否上传成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = default!;

    /// <summary>
    /// 向量维度
    /// </summary>
    public int EmbeddingDimension { get; set; }

    /// <summary>
    // 代理文本向量已废弃：统一模型后不再返回 ProxyKnowledgeId / ProxyEmbeddingDimension
}

/// <summary>
/// 知识库清理响应DTO
/// </summary>
public class KnowledgeCleanupResponseDto
{
    /// <summary>
    /// 删除的记录数量
    /// </summary>
    public int DeletedCount { get; set; }

    /// <summary>
    /// 是否清理成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = default!;

    /// <summary>
    /// 清理时间
    /// </summary>
    public DateTime CleanupTime { get; set; }
}
